/*
 * @Author: your name
 * @Date: 2021-08-31 20:04:25
 * @LastEditTime: 2021-08-31 20:18:44
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \briup\5-javascript\day07\11-排序.js
 */

var teachers = [
    { id: 3, name: "winnie", salary: 456 },
    { id: 7, name: "briup", salary: 123 },
    { id: 4, name: "jack", salary: 222 },
    { id: 9, name: "rose", salary: 777 }
];

function compare(v1, v2) {
    if (typeof v1 == 'object' && v2 instanceof Object) {
        if (v1.hasOwnProperty('id') && v2.hasOwnProperty('id')) {
            if (v1.id > v2.id) return 1;
            else if (v1.id < v2.id) return -1;
            else return 0;

        }
    }
}

function compare(v1, v2) {
    if (typeof v1 == 'object' && v2 instanceof Object) {
        if (v1.hasOwnProperty('name') && v2.hasOwnProperty('name')) {
            if (v1.name > v2.name) return 1;
            else if (v1.name < v2.name) return -1;
            else return 0;
        }
    }
}

function compare(v1, v2) {
    if (typeof v1 == 'object' && v2 instanceof Object) {
        if (v1.hasOwnProperty('salary') && v2.hasOwnProperty('salary')) {
            if (v1.salary > v2.salary) return 1;
            else if (v1.salary < v2.salary) return -1;
            else return 0;

        }
    }
}


//按id 排序
var r1 = teachers.sort(compare);
console.log(r1);
//按name排序
var r2 = teachers.sort(compare);
console.log(r2);
//按salary排序
var r3 = teachers.sort(compare);
console.log(r3);